package com.lagou.no1



import org.apache.flink.api.scala._
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}


object WordCount {
    def main(args: Array[String]): Unit = {
        //获取流环境
        val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
        //连接socket
        val socketDS: DataStream[String] = env.socketTextStream("node01",9999)
        //对数据进行压扁，根据空格切分
        val res: DataStream[(String, Int)] = socketDS.flatMap(_.split("\\s+"))
          .map((_, 1)) //每个单词组成k-v形式，k为单词，v为1
          .keyBy(_._1) //对相同的key进行分组
          .sum(1) //v相加，得到单词出现的数量
        res.print() //打印结果流
        env.execute() //运行
    }

}
